package com.aacar.service.impl;

import com.aacar.dao.MemberDao;
import com.aacar.dao.ProductDao;
import com.aacar.dto.ProductDto;
import com.aacar.entity.Member;
import com.aacar.entity.Product;
import com.aacar.service.ProductService;
import com.aacar.utils.DateUtil;
import com.aacar.utils.MathUtil;
import com.aacar.vo.ProductVo;
import com.aacar.vo.UserInfoVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;

@Repository("productService")
public class ProductServiceImpl implements ProductService {
	
	private Log logger = LogFactory.getLog(ProductServiceImpl.class);

	@Resource
	private ProductDao productDao;

	@Resource
	private MemberDao memberDao;


	@Override
	public ProductVo getProduct(ProductDto productDto) {
		Product productParam = new Product();
		BeanUtils.copyProperties(productDto,productParam);
		ProductVo productVo = new ProductVo();

		//获取标准价格
		Product product = productDao.getProduct(productParam);


		Member param = new Member();
		param.setMemberId(productDto.getUserId());
		UserInfoVo userVo = memberDao.getUserInfo(param);

		if (product!=null) {
			//默认查询银牌会员价格 用户显示
			productParam.setRankId("2");
		}
        //获取配置数据    获取会员等级价格 根据（车 ，省市区，小区，用户等级）获取
		Product userRankPriceProduct  = productDao.getUserRankPrice(productParam);
		//获取配置数据    获取预约优惠价格 根据（车 ，省市）获取
		Product bookPriceProduct = productDao.getBookPrice(productParam);
		//获取配置数据    获取尊享价格
		Product enjoyPriceProduct  = productDao.getEnjoyPrice(productParam);


		//配置数据-设置标准价格
		double productPrice = product==null?0.0:product.getProductPrice();
		long pid = product==null?0:product.getPid();
		productVo.setProductPrice(productPrice);

		//配置数据-设置用户等级价格
		double userRankPrice = userRankPriceProduct==null?0.0:userRankPriceProduct.getUserRankPrice();
		productVo.setUserRankPrice(userRankPrice);

		//配置数据-设置尊享价格
		double enjoyPrice = enjoyPriceProduct==null?0.0:enjoyPriceProduct.getEnjoyPrice();


		/////////////////////////////////////////////////////////////////////
		//开始 对最终价格进行逻辑处理
		/////////////////////////////////////////////////////////////////////
		double bookDiscPrice = bookPriceProduct==null?0.0:bookPriceProduct.getBookDiscPrice();
		double bookTime = bookPriceProduct==null?0.0:bookPriceProduct.getBookTime();
		//处理预约价格 判断预约开始时间小于当前时间 3小时30分（bookTime） 设置bookPrice为0
		Date startTime = DateUtil.str2Date(DateUtil.defaultPattern2,productDto.getBookStartTime());
		Date endTime = DateUtil.str2Date(DateUtil.defaultPattern2,productDto.getBookEndTime());
		Date tmpDate = DateUtil.addDateMinut(DateUtil.getNowDate(),(int)bookTime*60);
		//预约结束时间-开始时间小于2个小时50分 即小于170分钟 设置bookPrice为0
		if(DateUtil.caclurplusMinute(startTime,endTime)<170){
			if(startTime.getTime() < tmpDate.getTime()){
				bookDiscPrice = 0.0;
			}
		}
		//设置预约优惠价格
		productVo.setBookDiscPrice(bookDiscPrice);

		//最终产品价格
		double resultProductPrice = 0;

		//如果当前用户是银牌会员最终价格使用 userRankPrice
//		if(userRankPrice!=0 && "2".equals(userVo.getRankId())){
//			resultProductPrice = MathUtil.disposeAmount(userRankPrice+bookDiscPrice);
//		}else{
//			resultProductPrice = MathUtil.disposeAmount(productPrice+bookDiscPrice);
//		}
		if(userRankPrice != 0){
			resultProductPrice = MathUtil.disposeAmount(userRankPrice+bookDiscPrice);
		}else{
			resultProductPrice = MathUtil.disposeAmount(productPrice+bookDiscPrice);
		}



		//如果是首单 并且是银牌会员 并且有尊享价格
		if (userVo!=null && "2".equals(userVo.getRankId()) && userVo.getIsFirstOrder()!=null && userVo.getIsFirstOrder().equals("1")){
			logger.info("######## enjoyPriceProduct:"+enjoyPriceProduct);
			if(enjoyPriceProduct!=null){
				resultProductPrice = enjoyPrice;
			}
		}

		//如果查出来的尊享价格为null默认赋值为 会员价格
		if(enjoyPriceProduct==null){
			enjoyPrice = userRankPrice;
		}

		//设置尊享价格
		productVo.setEnjoyPrice(enjoyPrice);
        //设置最终价格
		productVo.setResultProductPrice(resultProductPrice);
		productVo.setProductId(pid);
		productVo.setUserRankId(userVo.getRankId());
		productVo.setIntegral(MathUtil.disposeAmount(Double.valueOf(userVo.getIntegral())));
		productVo.setIsFirstOrder(userVo.getIsFirstOrder());

		return productVo;

	}
}
